/**
 * Created by DukeCrushIt on 2015/8/17.
 */
//module game{
    class DateSelectPanel extends eui.Component{
        public constructor(){
            super();
            this.skinName = "skins.scene.DateSelectSkin";
        }

        public onClose(evt:egret.TouchEvent){
            DisplayController.instance().removeFromAlert(GameConst.GrayMask);
            DisplayController.instance().removeFromAlert(this);
        }

        private onOk(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            if( this.petList.selectedIndex != -1){
                var pet:Pet = this.petList.selectedItem;
                if( pet.petId != PetMgr.getInstance().curIndatePetId){
                    window["__indate"](pet.petId,"");
                }
            }
            this.onClose(null);
        }
        private img_close:eui.Image;
        private petList:eui.List;
        private img_ok:eui.Image;
        public partAdded(name:string, instance:any){
            super.partAdded(name, instance);
            if( name == "img_close"){
                this.img_close = instance;
                this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
            }else  if( name == "petList"){
                this.petList = instance;
                this.petList.itemRenderer = DateSelectItemRender;
                this.petList.addEventListener(egret.Event.CHANGE, this.onSelected, this);
            }else  if( name == "img_ok"){
                this.img_ok = instance;
                this.img_ok.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onOk, this);
            }
        }

        private onSelected(evt:egret.Event){
            SoundMgr.getInstance().playEffect(SoundMgr.CHOSE);
        }

        private listProvider:eui.ArrayCollection;
        public show(){
            DisplayController.instance().addToAlert(GameConst.GrayMask);
            DisplayController.instance().addToAlert(this);
            if( this.listProvider == null){
                this.listProvider = new eui.ArrayCollection();
                this.petList.dataProvider = this.listProvider;
            }
            this.updateList();
        }

        public updateListSelection(index:number){
            this.petList.selectedIndex = index;
        }

        public updateList(){
            this.listProvider.source = PetMgr.getInstance().pets;
        }

        public measure(){
            super.measure();
            this.x = GameConst.StageW - this.measuredWidth >> 1;
            this.y = GameConst.StageH - this.measuredHeight >> 1;
        }

        private static _instance:DateSelectPanel;
        public static getInstance():DateSelectPanel{
            if( DateSelectPanel._instance == null )
                DateSelectPanel._instance = new DateSelectPanel();
            return DateSelectPanel._instance;
        }
    }
//}